package com.foreknow.dao.impl;

import com.foreknow.dao.BusinessDao;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Admin;
import com.foreknow.model.business;
import com.foreknow.model.student;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BusinessDaoImpl extends BaseDAO implements BusinessDao {
    @Override
    public boolean save(business b1) {
        String sql="insert into business values(?,?,?,?,?,?,?)";
        String sql1 ="select * from business";
        EntityMapping map = mappingFactory.getMap(MappingFactory.BUSINESS_MAPPING);
        List<Object> list = jdbcTemplate.query(sql1, map);
        boolean IsRight=false;
        for (int i = 0; i <list.size() ; i++) {
            business b2 = (business) list.get(i);
            int businessId = b1.getBusinessId();
            int businessId1 = b2.getBusinessId();
            if (businessId==businessId1) {
                return false;
            }
        }
        int update = jdbcTemplate.update(sql, b1.getBusinessId(),
                b1.getPassword(), b1.getBusinessName(),
                b1.getBusinessAddress(), b1.getBusinessExplain(),
                b1.getStarPrice(), b1.getDeliveryPrice());
        if (update != 0) {
            return true;
        }
        return IsRight;
    }

    @Override
    public List<Object>  getAll() throws SQLException {
        String sql="select * from business";
        EntityMapping map = mappingFactory.getMap(MappingFactory.BUSINESS_MAPPING);
        List<Object> list = jdbcTemplate.query(sql, map);
        return list;
    }

    @Override
    public boolean deleteById(int id) throws SQLException {
        String sql="delete  from business where businessId=?";
        int update = jdbcTemplate.update(sql, id);
        return update==1;
    }

    @Override
    public business updateById(int id) throws SQLException {
        String sql="select * from business where id=?";
        business bus=null;
        EntityMapping map = mappingFactory.getMap(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jdbcTemplate.query(sql, map,id);
        if(list.size()>0) {
            bus = (business) list.get(0);
            return bus;
        }
        return null;
    }

    @Override
    public List<Object> getQueryById(int id) {
        String sql="select * from business where businessId=?";
        business bus=null;
        EntityMapping map = mappingFactory.getMap(MappingFactory.BUSINESS_MAPPING);
        List<Object> list = jdbcTemplate.query(sql, map,id);
        if (list!=null){
            return list;
        }
        return null;
    }



}
